// 数据统计模块
const DashboardModule = {
    // 初始化
    init() {
        this.loadStatistics();
    },

    // 加载统计数据
    loadStatistics() {
        CommonModule.apiRequest('/statistics')
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    const stats = data.data;
                    this.updateStatistics(stats);
                    this.updateDetailedStats(stats);
                }
            })
            .catch(error => {
                CommonModule.handleApiError(error, '加载统计数据');
            });
    },

    // 更新统计数据显示
    updateStatistics(stats) {
        const elements = {
            totalTeachers: document.getElementById('totalTeachers'),
            totalDepts: document.getElementById('totalDepts'),
            totalIndicators: document.getElementById('totalIndicators'),
            totalPositions: document.getElementById('totalPositions'),
            totalDeptEvaluations: document.getElementById('totalDeptEvaluations'),
            completedDeptEvaluations: document.getElementById('completedDeptEvaluations'),
            totalTeacherEvaluations: document.getElementById('totalTeacherEvaluations'),
            completedTeacherEvaluations: document.getElementById('completedTeacherEvaluations'),
            deptProgress: document.getElementById('deptProgress'),
            teacherProgress: document.getElementById('teacherProgress'),
            deptScoreCount: document.getElementById('deptScoreCount'),
            teacherScoreCount: document.getElementById('teacherScoreCount')
        };

        // 更新基础数值
        if (elements.totalTeachers) {
            elements.totalTeachers.textContent = stats.totalTeachers || 0;
        }
        if (elements.totalDepts) {
            elements.totalDepts.textContent = stats.totalDepts || 0;
        }
        if (elements.totalIndicators) {
            elements.totalIndicators.textContent = stats.totalIndicators || 0;
        }
        if (elements.totalPositions) {
            elements.totalPositions.textContent = stats.totalPositions || 0;
        }
        if (elements.totalDeptEvaluations) {
            elements.totalDeptEvaluations.textContent = stats.totalDeptEvaluations || 0;
        }
        if (elements.completedDeptEvaluations) {
            elements.completedDeptEvaluations.textContent = stats.completedDeptEvaluations || 0;
        }
        if (elements.totalTeacherEvaluations) {
            elements.totalTeacherEvaluations.textContent = stats.totalTeacherEvaluations || 0;
        }
        if (elements.completedTeacherEvaluations) {
            elements.completedTeacherEvaluations.textContent = stats.completedTeacherEvaluations || 0;
        }
        if (elements.deptScoreCount) {
            elements.deptScoreCount.textContent = stats.deptScoreCount || 0;
        }
        if (elements.teacherScoreCount) {
            elements.teacherScoreCount.textContent = stats.teacherScoreCount || 0;
        }

        // 更新进度条
        if (elements.deptProgress) {
            elements.deptProgress.style.width = (stats.deptProgress || 0) + '%';
        }
        if (elements.teacherProgress) {
            elements.teacherProgress.style.width = (stats.teacherProgress || 0) + '%';
        }
    },

    // 更新详细统计信息
    updateDetailedStats(stats) {
        // 更新部门类型统计
        if (stats.deptTypeStats) {
            this.updateDeptTypeStats(stats.deptTypeStats);
        }

        // 更新用户权限统计
        if (stats.userTypeStats) {
            this.updateUserTypeStats(stats.userTypeStats);
        }
    },

    // 更新部门类型统计
    updateDeptTypeStats(deptTypeStats) {
        const functionalElement = document.getElementById('functionalDepts');
        const collegeElement = document.getElementById('collegeDepts');
        const communityElement = document.getElementById('communityDepts');
        const otherElement = document.getElementById('otherDepts');

        if (functionalElement) functionalElement.textContent = deptTypeStats.functional || 0;
        if (collegeElement) collegeElement.textContent = deptTypeStats.college || 0;
        if (communityElement) communityElement.textContent = deptTypeStats.community || 0;
        if (otherElement) otherElement.textContent = deptTypeStats.other || 0;
    },

    // 更新用户权限统计
    updateUserTypeStats(userTypeStats) {
        const adminElement = document.getElementById('adminUsers');
        const schoolLeaderElement = document.getElementById('schoolLeaderUsers');
        const normalElement = document.getElementById('normalUsers');

        if (adminElement) adminElement.textContent = userTypeStats.admin || 0;
        if (schoolLeaderElement) schoolLeaderElement.textContent = userTypeStats.schoolLeader || 0;
        if (normalElement) normalElement.textContent = userTypeStats.normal || 0;
    }
};

// 导出模块
window.DashboardModule = DashboardModule; 